home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_13_05 / allison / person2.cpp < prev    next >
C/C++ Source or Header  |  1995-03-12  |  1KB  |  58 lines

  1. LISTING 20 - The implementation for Listing 19, which is missing
  2. an assignment operator and copy constructor
  3. // person2.cpp
  4. #include <iostream.h>
  5. #include <string.h>
  6. #include <new.h>
  7. #include "person2.h"
  8.  
  9. static char * clone(const char *s)
  10. {
  11.     // return a copy of a string on the heap
  12.     char * p = new char[strlen(s) + 1];
  13.     return strcpy(p,s);
  14. }
  15.  
  16. Person::Person() : birth(Date(0,0,0))
  17. {
  18.     last = clone("");
  19.     first = clone("");
  20.     ssn = clone("");
  21. }
  22.  
  23. Person::Person(const char * l, const char * f,
  24.                const Date& b, const char * s)
  25.   : birth(b)
  26. {
  27.     last = clone(l);
  28.     first = clone(f);
  29.     ssn = clone(s);
  30. }
  31.  
  32. Person::~Person()
  33. {
  34.     delete [] last;
  35.     delete [] first;
  36.     delete [] ssn;
  37. }
  38.  
  39. ostream& operator<<(ostream& os, const Person& p)
  40. {
  41.     os << '{'
  42.        << p.last << ','
  43.        << p.first << ','
  44.        << '[' << p.birth << ']' << ','
  45.        << p.ssn
  46.        << '}';
  47.     return os;
  48. }
  49.  
  50. bool Person::operator==(const Person & p) const
  51. {
  52.     return strcmp(last,p.last) == 0 &&
  53.            strcmp(first,p.first) == 0 &&
  54.            birth == p.birth &&
  55.            strcmp(ssn,p.ssn) == 0;
  56. }
  57.  
  58.